package bupt.party.online.listener;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.apache.struts2.ServletActionContext;

import bupt.party.online.model.Student;
import bupt.party.online.util.SessionConstants;

public class ActiveUserListener implements HttpSessionListener {

	@Override
	public void sessionCreated(HttpSessionEvent event) {
		
//		System.out.println("I am in ActiveUserListener::sessionCreated");
		/*HttpSession session = event.getSession();
		session.setMaxInactiveInterval(30);*/
		
		
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent event) {
//		System.out.println("I am in ActiveUserListener::sessionDestroyed");
		
		
		
		HttpSession  session = event.getSession();
		
		Integer level = (Integer) session.getAttribute(SessionConstants.LEVEL);
		
		if(level == null || !level.equals(SessionConstants.STU_LEVEL)) return;
		
		Student student = (Student) session.getAttribute(SessionConstants.SESSION_USER);
		
		if(student==null){
			return;
		}
		
		String studentNo = student.getStudentNo();
		try{
			Map appMap = ServletActionContext.getContext().getApplication();
			Map sessionMap = (Map)appMap.get("sessionMap");
			if(sessionMap==null){
				sessionMap = new HashMap();
				appMap.put("sessionMap", sessionMap);
			}
			
			sessionMap.remove(studentNo);
			
		}catch(Exception e){
//			e.printStackTrace();
		}
		
	}

}
